package com.taxiApp.util;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

public class BaseResponse {

    public static final BaseResponse SUCCESS = new BaseResponse(null, true, null);


    @Getter @Setter private Meta          meta;
    @Getter @Setter private Object        data;

    public BaseResponse() {
        this(null, false, null, null);
    }

    public BaseResponse(Integer code, String msg) {
        this.meta=new Meta(code,msg);
    }

    public BaseResponse(boolean result, String msg) {
        this.meta=new Meta(result,msg);
    }

    public BaseResponse(Integer code, boolean result, String msg) {
        this.meta=new Meta(code,result,msg);
    }

    public BaseResponse(Integer code, boolean result, String msg,Object data) {
        this.meta=new Meta(code,result,msg);
        this.data=data;
    }

    public boolean getResult(){
        return meta.isResult();
    }

    public void setResult(boolean result){
        meta.setResult(result);
    }

    public Integer getCode(){
        return meta.getCode();
    }

    public void setCode(Integer code){
        meta.setCode(code);
    }

    public String getMsg(){
        return meta.getMsg();
    }

    public void setMsg(String msg){
        meta.setMsg(msg);
    }

    public BaseResponse success(){
        this.setMsg("操作成功");
        this.setResult(true);
        return this;
    }

    public BaseResponse success(String msg){
        this.setMsg(msg);
        this.setResult(true);
        return this;
    }

    public BaseResponse failed(){
        this.setMsg("操作失败");
        this.setResult(false);
        return this;
    }

    public BaseResponse failed(String msg){
        this.setMsg(msg);
        this.setResult(false);
        return this;
    }

    public Object getObjectFromDataByKey(String key){
        if(!(data instanceof HashMap)){
            throw new RuntimeException("响应数据不是HashMap类型");
        }
        return ((HashMap)data).get(key);
    }

    public void putKeyAndValue(String key, Object value){
        if(data==null || !(data instanceof HashMap)){
            data = new HashMap<String, Object>();
        }
        ((HashMap)data).put(key, value);
    }


    static public void main(String[] args) {
        BaseResponse response = new BaseResponse();
        response.putKeyAndValue("id", 5);
        System.out.println(JSON.toJSONString(response.success()));

        BaseResponse response1 = new BaseResponse();
        System.out.println(JSON.toJSONString(response1.failed()));
    }

    public static class Builder{
        Integer                     code;
        boolean                     result;
        String                      msg;
        Object                      data;
        Map<String, Object> mapData;

        public Builder code(Integer code) {
            this.code = code;
            return this;
        }

        public Builder result(boolean result) {
            this.result = result;
            return this;
        }

        public Builder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public Builder mapData() {
            this.mapData = new HashMap<String, Object>();
            return data(this.mapData);
        }

        public Builder putMapData(String key, Object value) {
            if (this.mapData == null) {
                throw new IllegalStateException("call mapData() first");
            }
            this.mapData.put(key, value);
            return this;
        }

        public BaseResponse build() {
            return new BaseResponse(this.code, this.result, this.msg, this.data);
        }
    }
}
